// pages/projectcreat/projectcreat.ts



Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    currenttime:new Date().getTime(),
    projectdetail:{start:"请选择时间",end:"请选择时间"},
    startpicker:false,
    endpicker:false,
    date:new Date().getFullYear()+"-"+new Date().getMonth()+"-"+new Date().getDay(),
    tags:[],
    colors:[["#165DFF","#E8F3FF"],["#00B42A","#E8FFEA"],["#4E5969","#F2F3F5"]],
    onaddtag:false,
    fileList:[],
    introduction:"",
    expect:"",
    organizer:"",
    period:"",
    location:"",
    rop:"",
    scop:0,
    count:0,
  },
  getdatetime(e){
    var id=e.currentTarget.id
    var datetimestr=JSON.stringify(new Date(e.detail))
    var datetimearr=datetimestr.split("T")
    var datetimearr1=datetimearr[1].split(".")
    var datetime=(datetimearr[0]+" "+datetimearr1[0]).split('"')[1]
    var key="projectdetail."+id
    this.setData({
      [key]:datetime
    })
    this.unshowpicker()
  },
  unshowpicker(){
    this.setData({
      startpicker:false,
      endpicker:false
    })
  },
  showpicker(e){
    var id=e.currentTarget.id
    this.setData({
      [id]:true
    })
  },
  datechage:function(e){
    this.setData({
      date:e.detail.value
    })
  },
  showaddtag(e){
    this.setData({
      onaddtag:true
    })
  },
  addtag(e){
    var value=e.detail.value
    var tags=this.data.tags    
    tags.push(value)
    this.setData({
      tags:tags,
      onaddtag:false
    })
  },
  subtag(e){
    var index=e.currentTarget.dataset.index
    var tags=this.data.tags
    tags.splice(index,1)
    this.setData({
      tags:tags,
    })
  },
  afterRead(e){
    var files=e.detail.file
    var fileList=this.data.fileList
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      file.url=file.path
      fileList.push(file)
    }
    this.setData({
      fileList:fileList
    })
  },
  del(event){
    let id = event.detail.index   //能获取到对应的下标
    let fileList = this.data.fileList  //这里是前端页面展示的数组
    fileList.splice(id,1)     //根据下标来删除对应的图片
    this.setData({
      fileList:fileList,  //在这里进行重新赋值  删除后 图片剩几张就相当于给后台传几张
    })
 },
 getprojectdetail(e){
   var id=e.currentTarget.id
   this.setData({
   [id]:e.detail
   })
 },
 async sumbit(){
   var that=this
   var token=getApp().globalData.accessToken
    wx.showModal({
      title:"确定提交项目吗",
      success:async function(res){
        if(res.confirm){
           let fileList=that.data.fileList
          var files=[];  //构建的promise数组
           fileList.forEach((item,index)=>{  //item为图片的url
            var file={filePath:item.tempFilePath}
            files.push(file)
           })
           const field={
            end:that.data.projectdetail.start, 
            name:that.data.name,
            tag:that.data.tags,
            organizer:that.data.organizer,
            location:that.data.location,
            introduction:that.data.introduction,
            rop:that.data.rop,
            period:that.data.period,
            count:that.data.count,
            scope:that.data.scop,
            expect:that.data.expect}
            var fields = Object.keys(field).map(key => ({
              name: key,
              value: field[key]
            }))
            files.map((item, index) => {
              return Object.assign(item, {
                name: "files",filePath:item.filePath })
            });
            axios.upload('/project',fields,files)
          await wx.p.showToast({
              title:"提交成功"
            })
           wx.navigateBack({
             delta:1
           })
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})